package hxm.monads;

/**
 * A monad for working with all kinds of Iterable objects.
 * 
 * @author Simon Krajewski
 */

class IterableMonad 
{
	inline static public function bind<A, B>(a:Iterable<A>, f:A -> Iterable<B>):Iterable<B>
	{
		var b = map(a, f);
		
		var ret = new List<B>();
		
		for (c in b)
			for (d in c)
				ret.add(d);
		
		return ret;
	}
	
	inline static public function map<A, B>(a:Iterable<A>, f:A->B):Iterable<B>
	{
		var ret = new List<B>();
		for (i in a)
			ret.add(f(i));
		return ret;
	}
	
	inline static public function yield<A>(a:A):Iterable<A>
	{
		var ret = new List();
		ret.add(a);
		return ret;
	}

	static public var zero = ListMonad.zero;
}